#include "./../../lvgl/lvgl.h"
#include "./../../lvgl/demos/lv_demos.h"
#include "./../../lv_drivers/display/fbdev.h"
#include "./../../lv_drivers/indev/evdev.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>

#include "./../../inc/homePage.h"
#include "./../../inc/album.h"
#include "./../../inc/page_manager.h"
#include "./../../inc/setting.h"
#include "./../../inc/login.h"
#include "./../../inc/music.h"

static lv_timer_t * timer    = NULL;
static int angle             = 0;
static lv_obj_t * music_img  = NULL;
static lv_obj_t * music_name = NULL;
lv_obj_t * music_all_screen  = NULL;
int music_mode               = CLOSE_MODE;

lv_obj_t * progress_bar     = NULL;
lv_obj_t * music_time_label = NULL;

static void change_angle();

void music_init(void)
{
    if(music_all_screen != NULL) {
        lv_obj_del(music_all_screen);
        music_all_screen = NULL;
    }

    music_all_screen = lv_obj_create(NULL);
    lv_obj_set_style_pad_all(music_all_screen, 0, LV_PART_MAIN);           // 设置内边距
    lv_obj_set_style_border_width(music_all_screen, 0, LV_PART_MAIN);      // 设置边框宽度
    lv_obj_set_style_bg_opa(music_all_screen, LV_OPA_0, LV_PART_MAIN);     // 设置背景完全透明
    lv_obj_set_style_border_opa(music_all_screen, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
    lv_obj_set_style_shadow_opa(music_all_screen, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明

    lv_obj_t * img = lv_img_create(music_all_screen);

    // lv_img_set_src(img, "S:res/ui/music_bg.png");
    lv_img_set_src(img, "S:res/img/car_bg.png");

    lv_obj_set_size(img, 800, 480);
    lv_obj_set_style_border_width(img, 0, LV_PART_MAIN);      // 设置边框宽度
    lv_obj_set_style_bg_opa(img, LV_OPA_0, LV_PART_MAIN);     // 设置背景完全透明
    lv_obj_set_style_border_opa(img, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
    lv_obj_set_style_shadow_opa(img, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明

    lv_obj_t * music_screen = lv_obj_create(music_all_screen);
    lv_obj_set_size(music_screen, 800, 390);
    lv_obj_set_pos(music_screen, 0, 40);
    lv_obj_set_style_pad_all(music_screen, 0, LV_PART_MAIN);
    lv_obj_set_style_radius(music_screen, 0, LV_PART_MAIN);
    lv_obj_set_style_border_width(music_screen, 0, LV_PART_MAIN);
    lv_obj_set_style_bg_opa(music_screen, LV_OPA_0, LV_PART_MAIN);     // 设置背景完全透明
    lv_obj_set_style_border_opa(music_screen, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
    lv_obj_set_style_shadow_opa(music_screen, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明

    lv_obj_clear_flag(music_screen, LV_OBJ_FLAG_SCROLLABLE); // 不可滚动

    show_status_bar(music_all_screen);
    show_taskbar(music_all_screen);

    play_music_init(music_screen);
    navigate_to(music_all_screen);
}

void play_music_init(lv_obj_t * parent)
{
    lv_obj_t * music_picture = lv_obj_create(parent);
    lv_obj_set_size(music_picture, 250, 250);
    lv_obj_align_to(music_picture, parent, LV_ALIGN_TOP_MID, 0, 5);

    lv_obj_set_style_radius(music_picture, LV_RADIUS_CIRCLE, LV_PART_MAIN); // 设置圆形样式
    lv_obj_set_style_clip_corner(music_picture, true, LV_PART_MAIN);

    music_img = lv_img_create(music_picture);

    lv_img_set_src(music_img, music_imgs[current_song]);

    lv_obj_center(music_img);
    lv_obj_clear_flag(music_picture, LV_OBJ_FLAG_SCROLLABLE); // 不可滚动
    lv_obj_set_style_border_width(music_picture, 0, LV_PART_MAIN);
    lv_obj_set_style_border_opa(music_picture, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
    lv_obj_set_style_shadow_opa(music_picture, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明

    lv_img_set_pivot(music_img, 125, 125);

    if(timer == NULL) timer = lv_timer_create(change_angle, 10, NULL);

    lv_timer_set_repeat_count(timer, -1);
    if(music_mode == CLOSE_MODE) lv_timer_pause(timer);

    // lv_obj_t * music_name_card = lv_label_create(parent);
    // lv_obj_set_style_radius(music_name_card, 6, LV_PART_MAIN);
    // lv_obj_set_style_bg_opa(music_name_card, LV_OPA_0, LV_PART_MAIN); // 设置背景完全透明
    // lv_obj_set_style_border_width(music_name_card, 0, LV_PART_MAIN);  // 移除边框
    // lv_obj_clear_flag(music_name_card, LV_OBJ_FLAG_SCROLLABLE);       // 不可滚动

    music_name = lv_label_create(parent);
    lv_label_set_text(music_name, music_names[current_song]);
    lv_obj_set_style_bg_opa(music_name, LV_OPA_0, LV_PART_MAIN); // 设置背景完全透明
    lv_obj_set_style_border_width(music_name, 0, LV_PART_MAIN);  // 移除边框
    lv_obj_clear_flag(music_name, LV_OBJ_FLAG_SCROLLABLE);       // 不可滚动

    LV_FONT_DECLARE(SourceHanSansCN_Bold_20);
    static lv_style_t label_style;
    lv_style_init(&label_style); // 初始化样式

    lv_style_set_text_align(&label_style, LV_TEXT_ALIGN_CENTER); // 水平居中 + 垂直居中
    lv_style_set_text_font(&label_style, &SourceHanSansCN_Bold_20);
    lv_style_set_text_color(&label_style, lv_color_hex(WHITE));
    lv_style_set_pad_top(&label_style, 2);
    lv_style_set_pad_bottom(&label_style, 2);
    lv_style_set_pad_left(&label_style, 5);
    lv_style_set_pad_right(&label_style, 5);

    // lv_obj_center(music_name);

    lv_obj_add_style(music_name, &label_style, LV_PART_MAIN);
    lv_obj_align(music_name, LV_ALIGN_BOTTOM_MID, 0, -100);
    lv_obj_clear_flag(music_name, LV_OBJ_FLAG_SCROLLABLE); // 禁止标签滚动

    // ------------------------------------进度条------------------------------------

    lv_obj_t * progress_cont = lv_obj_create(parent);
    lv_obj_set_size(progress_cont, 520, 60);
    lv_obj_align(progress_cont, LV_ALIGN_BOTTOM_MID, -30, -50);
    lv_obj_set_style_bg_opa(progress_cont, LV_OPA_0, LV_PART_MAIN);
    lv_obj_set_style_border_width(progress_cont, 0, LV_PART_MAIN);
    lv_obj_clear_flag(progress_cont, LV_OBJ_FLAG_SCROLLABLE);

    // 进度条
    progress_bar = lv_slider_create(progress_cont);
    lv_obj_set_size(progress_bar, 350, 15);
    lv_obj_align(progress_bar, LV_ALIGN_TOP_MID, 0, 5);
    lv_slider_set_range(progress_bar, 0, 100);
    lv_slider_set_value(progress_bar, 0, LV_ANIM_OFF);

    // 设置进度条样式 - 头部为平的
    lv_obj_set_style_bg_color(progress_bar, lv_color_hex(0x4A4A4A), LV_PART_MAIN);
    lv_obj_set_style_bg_color(progress_bar, lv_color_hex(0x2B7AFE), LV_PART_INDICATOR);

    lv_obj_set_style_radius(progress_bar, 5, LV_PART_MAIN);      // 背景轨道保持圆角
    lv_obj_set_style_radius(progress_bar, 0, LV_PART_INDICATOR); // 进度指示器设为平头

    lv_obj_set_style_opa(progress_bar, LV_OPA_0, LV_PART_KNOB); // 隐藏旋钮并禁止交互

    // 移除可点击和可滚动标志
    lv_obj_clear_flag(progress_bar, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_clear_flag(progress_bar, LV_OBJ_FLAG_SCROLLABLE);

    // 时间
    music_time_label = lv_label_create(progress_cont);
    lv_obj_align_to(music_time_label, progress_bar, LV_ALIGN_OUT_RIGHT_MID, 10, 0);
    lv_label_set_text(music_time_label, "0:00 / 0:00");
    lv_obj_set_style_text_color(music_time_label, lv_color_white(), 0);
    lv_obj_set_style_text_font(music_time_label, &lv_font_montserrat_14, 0);

    update_time_label();
}

static void change_angle()
{
    angle += 10;
    lv_img_set_angle(music_img, angle);
}

void pause_timer()
{
    if(timer) {
        lv_timer_pause(timer);
    }
    if(progress_timer) {
        lv_timer_pause(progress_timer);
    }
    music_mode = CLOSE_MODE;
}

void resume_timer()
{
    if(timer) {
        lv_timer_resume(timer);
    }
    if(progress_timer && IS_PLAYING == OPEN_MODE) {
        lv_timer_resume(progress_timer);
    }
    music_mode = OPEN_MODE;
}

void change_music_picture()
{
    if(music_img) {
        pause_timer();
        // 立即更新图片
        lv_img_set_src(music_img, music_imgs[current_song]);
        angle = 0;
        lv_img_set_angle(music_img, angle);
        lv_label_set_text(music_name, music_names[current_song]);
        if(IS_PLAYING == OPEN_MODE) {
            resume_timer();
        }
    }
    change_home_music_card();
}
